<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as  echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "1100px",
    },
    height: {
      type: String,
      default: "650px",
    },
    chartX: {
      type: Array,
    },
    chartdata: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      var that = this;
      this.initChart(that.chartX, that.chartdata);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(data_name, data_value) {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        title:{
          text: "人身伤害事故数量统计",
          left: "center",
          top: "3%",
          textStyle: {
            fontSize: "18px",
            fontWeight: "600",
            color: "#333"
          }
        },
        legend: {
          data: ['死亡', '重伤', '轻伤'],
          right: "5%",
          top: "3%"
        },
        tooltip: {
          trigger: "axis",
        },
        grid: {
          top: "15%",
          left: "8%",
          right: "8%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          axisTick: { show: false },
          data: data_name
        },
        yAxis: {
          type: 'value'
        },
        series: data_value
      });
    },
  },
};
</script>
